<template>
	<view>
		<view>
			<z-paging ref="paging" v-model="listData" :show-scrollbar="false" :loading-more-enabled="false" @query="queryList">
				<view class="car_home_box" v-for="(car,idx) in listData" :key="idx">
					<view class="car_home_name" v-if="car.list.length>0">
						<view class="select_box" @click="clickHomeSelect(car)">
							<uni-icons v-if="car.selected" type="checkbox-filled" color="#FD5052" size="26"></uni-icons>
							<uni-icons v-else type="circle" color="#999999;" size="26"></uni-icons>					
						</view>
						<view style="margin-left: 10rpx;">{{car.orchardName}}</view>
					</view>
					<view class="car_home_foods">
						<view class="car_food_item" v-for="(item,index) in car.list" :key="index">
							<view class="select_box" @click="clickSingleGood(car,item)">							
								<uni-icons v-if="item.selected" type="checkbox-filled" color="#FD5052" size="26"></uni-icons>
								<uni-icons v-if="!item.selected" type="circle" color="#999999;" size="26"></uni-icons>	
							</view>
							<view class="car_food_info">
								<view class="car_food_img" @click="chooseSpuHandle(item.spu.id)">
									<image class="food_img" :src="item.spu.picUrl" mode="aspectFill"></image>
								</view>
								<view class="car_food_right">
									<view class="car_food_top">
										<view class="car_food_left">
											<view class="car_food_name" @click="chooseSpuHandle(item.spu.id)">{{item.spu.name}}</view>
											<view class="car_food_remark">已选: {{parseSkuName(item.sku.properties)}}</view>
										</view>											
									</view>
									<view class="car_food_bottom">
										<view class="car_food_price">￥{{parsePrice(item.sku.price)}}</view>
										<view class="info_num">
											<uni-number-box v-model="item.count" :step="1" @change="changeBuyNum($event, item.id)"/>										
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</z-paging>	
		</view>		
		
		<!-- 底部fixed Next部分 -->
		<view class="height100"></view>
		<view class="fixed_footer_next" v-if="listData.length > 0">
			<view class="footer_select_box" @click="clickAllSelect">
				<uni-icons v-if="isAllChecked" type="checkbox-filled" color="#FD5052" size="26"></uni-icons>
				<uni-icons v-if="!isAllChecked" type="circle" color="#999999;" size="26"></uni-icons>	
				<view class="all-select"><text>全选（{{totalNumber}}件）</text></view>	
				<view class="next_price">合计：￥{{totalPrice > 0 ? parsePrice(totalPrice) : '0.00'}}</view>
			</view>			
			<view class="next_box">				
				<view class="next_btn">
					<button type="default" @click="toBuy">下单结算</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $U from '@/common/free-lib/util.js';
	import $H from '@/common/free-lib/request.js';
	import zPaging from "@/uni_modules/z-paging/components/z-paging/z-paging.vue";
	import ZPMixin from '@/uni_modules/z-paging/components/z-paging/js/z-paging-mixin.js'
	
export default{
	components: {
		zPaging,
	},
	mixins: [ZPMixin],
	data(){
		return{
			carList: [],
			phoneHeight:0,
			totalNumber: 0,//购物车商品总数量
			totalPrice: 0,//购物车商品总价格
			isAllChecked: false,//是否全选  0：不是  1：是
			scrollerHeight: 350,
			listData: [],
			cartList: [],
			queryForm: {
				pageNo: 1,
				pageSize: 10
			}
		}
	},
	props:{
		listDatas:{
			type: Array,
			default: () => {
				return []
			}
		}
	},
	onLoad(e) {
		// console.log(88888)
	},
	onShow() {
		this.getCartList();
	},
	onReady() {	
		var height
		var _this = this;
		uni.getSystemInfo({
			success: function(res) {
				_this.phoneHeight = res.windowHeight;
				_this.scrollerHeight = res.windowHeight;
				_this.$nextTick(() => {
					let view = uni.createSelectorQuery().select("#foot"); //局部滑块
					view.boundingClientRect(data => {
						if(data && data.height) {
							_this.topHeight = data.height;
							height = _this.phoneHeight - _this.topHeight;
							_this.scrollerHeight = height;
						}							
					}).exec();
				})
			},
		}); 
		
	},
	options: {
		styleIsolation: 'shared'
	},
	methods:{
		chooseSpuHandle(id) {
			uni.navigateTo({url: '../../../pages/index/product-detail?pid=' + id});
		},
		queryList(pageNo, pageSize) {
			this.getCartList();
		},
		getCartList() {
			let _this = this;
			if($U.isLogin()) {
				$H.get('/trade/cart/myCartList', this.queryForm).then(res => {
					if(res) {
						_this.cartList = res;
						_this.$refs.paging.complete(res);
						_this.sumTotalPrice();
					}
				})
			}
			else {
				let url = '/pages/tabbar/find/find';
				uni.navigateTo({url: '/pages/common/login/login?from=' + url})
			}
		},
		parseSkuName(list) {
			// console.log(list)
			let label = ''
			if(list && list.length > 0) {
				list.forEach((p,i) => {
					label += p.valueName;
					if(i < (list.length - 1) ) {
						label += '、';
					}
				})
			}
			return label;
		},
		parsePrice(price) {
			return $U.fenToYuan(price);
		},
		submitSelect(ids, s) {
			let _this = this;
			let data = {ids: ids, selected: s};
			$H.put('/trade/cart/update-selected', data).then(res => {
				_this.getCartList();
			})
		},
		changeBuyNum(val, id) {
			let _this = this;
			if(val > 0) {
				let data = {id: id, count: val};
				$H.put('/trade/cart/update-count', data).then(res => {
					_this.getCartList();
				})
			}
			else {
				let ids = [];
				ids.push(id);
				uni.showModal({
					title: '提示',
					content: '确认移除购物车？',
					success: function (res) {
						if (res.confirm) {
							let data = {ids: ids, selected: false}
							$H.post('/trade/cart/delete', data).then(res => {
								_this.getCartList();
								_this.$store.commit('loadUserData');
							})
						} else if (res.cancel) {
							_this.getCartList();
							return;
						}
					}
				});	
			}
		},
		toBuy(){
			let _this = this;
			let dataList = [];
			if(this.cartList && this.cartList.length > 0) {
				for(let i in _this.cartList){
					for(let j in _this.cartList[i].list){						
						if(_this.cartList[i].list[j].selected) {
							dataList.push(_this.cartList[i].list[j]);
						}
					}
				}
			}
			if(dataList.length > 0) {
				if(dataList.length <= 10) {
					this.$emit('toBuy', dataList)
				}
				else {
					uni.showToast({
						title: '抱歉，一次下单只能购买最多10个商品',
						icon: 'none'
					});
				}
			}
			else {
				uni.showToast({
					title: '请选择需要购买的商品',
					icon: 'none'
				});
			}
			
		},
		sumTotalPrice(){
			// 计算添加到购物车的数量和价格
			let total = 0;
			let allPrice = 0;
			let all = 0;
			let _this = this;
			if(this.cartList && this.cartList.length > 0) {
				for(let i in _this.cartList){
					for(let j in _this.cartList[i].list){
						all++;
						if(_this.cartList[i].list[j].selected) {
							total++;
							allPrice += _this.cartList[i].list[j].sku.price * _this.cartList[i].list[j].count;
						}
					}
				}
			}
			
			this.isAllChecked = all === total ? true : false;
			
			this.totalNumber = total;
			this.totalPrice = allPrice;
		},
		clickHasSelect(){
			// 是否有选中商品
			let result1 = '';
			result1 = this.carList.find(item=>{
				return item.checked
			})
			if(result1 == undefined || result1 == 'undefined'){
				this.isAllChecked = 0;
			}
		},
		clickHomeSelect(orchard){
			if(orchard && orchard.list && orchard.list.length > 0) {
				let selected = !orchard.selected;
				orchard.selected = selected;
				let ids = [];
				orchard.list.forEach(item => {
					ids.push(item.id);
				})
				this.submitSelect(ids, selected);
			}			
		},
		clickSingleGood(car,item){
			if(car && item) {
				let selected = !item.selected;
				item.selected = selected;
				let ids = [];
				ids.push(item.id)
				this.submitSelect(ids, selected);
			}			
		},
		clickAllSelect(){
			let allSelect = !this.isAllChecked;
			if(this.cartList && this.cartList.length > 0) {
				let _this = this;
				let ids = [];
				for(let i in _this.cartList){
					for(let j in _this.cartList[i].list){
						ids.push(_this.cartList[i].list[j].id);
					}
				}
				if(ids.length > 0) {
					this.submitSelect(ids, allSelect);
				}
			}
		}
	}
}
</script>

<style lang="scss" scoped>
	
	
	/deep/ .z-paging-content-fixed {
		bottom: 100rpx;
	}
	
	.height100{
		height: 150rpx;
	}
	.select_box{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		.select_icon{
			display: block;
			width: 34rpx;
			height: 34rpx !important;
			border: 2rpx solid #cccccc;
			border-radius: 50%;
			margin-right: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			image{
				width: 34rpx;
				height: 38rpx !important;
			}
		}
		.del_all_icon{
			width: 24rpx;
			height: 30rpx;
			margin-left: 20rpx;
		}
	}
	
	.no_car_data{
		text-align: center;
		padding-top: 250rpx;
		color: #333333;
		font-size: 24rpx;
		text{
			color: #cc1f26;
			margin-left: 5rpx;
		}
	}
	
	.car_home_box{
		font-size: 24rpx;
		.car_home_name{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			padding: 10rpx 20rpx;
			box-sizing: border-box;
			view{				
				font-size: 28rpx;
			}
		}
		.car_home_foods{
			background-color: #ffffff;
			.car_food_item{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				padding: 20rpx;
				box-sizing: border-box;
				border-bottom: 2rpx solid #eaeaea;
				&:first-of-type{
					border-top: 2rpx solid #eaeaea;
				}
				.car_food_info{
					display: flex;
					justify-content: space-between;
					align-items: center;
					width: 100%;
					.car_food_img{
						width: 160rpx;
						height: 160rpx;
						border: 2rpx solid #eceff3;
						box-sizing: border-box;
						margin-right: 20rpx;
						border-radius: 10rpx;
						overflow: hidden;
						.food_img{
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}
					.car_food_right{
						width: 72%;
						.car_food_top{
							display: flex;
							justify-content: space-between;
							align-items: flex-start;
							.car_food_left{
								width: 90%;
								.car_food_name{
									font-size: 28rpx;
									margin-bottom: 5rpx;
									color: #222222;
									display: -webkit-box;
									-webkit-box-orient: vertical;
									-webkit-line-clamp: 2; /* 定义文本的行数 */
									overflow: hidden;
									text-overflow: ellipsis;
								}
								.car_food_remark{
									color: #868ea4;
									font-size: 24rpx;
									height: 40rpx;
									line-height: 40rpx;
									margin-top: 10rpx;
								}
							}
							.car_del_icon{
								width: 24rpx;
								height: 30rpx;
							}
						}
						.car_food_bottom{
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 10rpx;
							.info_num {
								width: 140rpx;
								height: 60rpx;
								line-height: 60rpx;
								align-items: center;
								display: flex;
								margin-right: 70rpx;								
								.number {
									text-align: center;
								}
								.plus {
									width: 40rpx;
									height: 20rpx;
									display: flex;
									line-height: 20rpx;
									padding: 10rpx;
									border: 2rpx solid #FD5052;
								}
							}
							.car_food_price{
								color: #FD5052;
								font-size: 28rpx;
							}
						}
					}
				}
			}
		}
	}
	
	.fixed_footer_next{
		background-color: #ffffff;
		position: fixed;
		width: 100%;
		left: 0;
		bottom: 0;
		z-index: 10;
		/* #ifdef H5 */
		bottom: calc(var(--window-bottom));
		/* #endif */
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		box-sizing: border-box;
		font-size: 26rpx;
		border-top: 2rpx solid #eaeaea;
		.footer_select_box {
			width: 60%;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			.all-select {
				width: auto;
				white-space: nowrap;
			}
			.next_price{
				width: auto;
				font-size: 16px;
				color: #FD5052;
				white-space: nowrap;
			}
		}
		
		.next_box{
			width: 30%;
			display: flex;
			justify-content: flex-end;
			align-items: center;			
			.next_btn{
				button{
					width: 188rpx;
					// min-width: 188rpx;
					// padding: 0 30rpx;
					box-sizing: border-box;
					height: 60rpx;
					line-height: 60rpx;
					color: #ffffff;
					background-color: #FD5052;
					font-size: 28rpx;
					border: none;
					border-radius: 10rpx;
					margin-left: 15rpx;
					padding: 0;
				}
			}
		}
	}
</style>
